package com.frkj.order.model;

import com.frkj.order.cache.BusinessCache;
import com.frkj.order.vo.FullQueueInfo;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import java.util.*;

/**
 * Created by Administrator on 2016/6/27.
 *
 +---------------+--------------+------+-----+---------+----------------+
 | Field         | Type         | Null | Key | Default | Extra          |
 +---------------+--------------+------+-----+---------+----------------+
 | id            | int(11)      | NO   | PRI | NULL    | auto_increment |
 | number        | varchar(30)  | NO   |     | NULL    |                |
 | user_type     | int(11)      | NO   |     | NULL    |                |
 | open_id       | varchar(100) | YES  |     | NULL    |                |
 | business_id   | int(11)      | NO   |     | NULL    |                |
 | business_name | varchar(100) | NO   |     | NULL    |                |
 | business_tag  | varchar(30)  | NO   |     | NULL    |                |
 | operator_id   | int(11)      | YES  |     | NULL    |                |
 | operator_name | varchar(100) | YES  |     | NULL    |                |
 | status        | int(11)      | NO   |     | NULL    |                |
 | create_time   | datetime     | NO   |     | NULL    |                |
 | start_time    | datetime     | YES  |     | NULL    |                |
 | end_time      | datetime     | YES  |     | NULL    |                |
 | cost_time     | int(11)      | YES  |     | NULL    |                |
 | window        | int(11)      | YES  |     | NULL    |                |
 +---------------+--------------+------+-----+---------+----------------+
 */
public class HistoryOrder extends Model<HistoryOrder> {

    public static final HistoryOrder me = new HistoryOrder();

    /**
     * 获取全部业务所有状态统计信息
     * @return
     */
    public List<FullQueueInfo> getFullQueueInfo(Date beginDate, Date endDate) {
        List<Business> businessList = BusinessCache.getBusinessList();
        Map<String, FullQueueInfo> fullQueueInfoMap = new LinkedHashMap<String, FullQueueInfo>();
        for(Business business : businessList) {
            fullQueueInfoMap.put(business.getStr("name"), new FullQueueInfo(business.getStr("name")));
        }

        List<Record> recordList = Db.find("select business_name, status, count(number) as cnt from historyorder where create_time>? and create_time<? group by business_name, status", beginDate, endDate);
        for(Record record : recordList) {
            String businessName = record.getStr("business_name");
            int status = record.getInt("status");
            long count = record.getLong("cnt");
            fullQueueInfoMap.get(businessName).setCount(status, count);
        }

        FullQueueInfo[] fullQueueInfos = new FullQueueInfo[fullQueueInfoMap.size()];
        fullQueueInfoMap.values().toArray(fullQueueInfos);
        return Arrays.asList(fullQueueInfos);
    }
}
